package com.tedu.egspringbootmvc.controller;

import com.tedu.egspringbootmvc.common.result.JsonResult;
import com.tedu.egspringbootmvc.common.result.StatusCode;
import com.tedu.egspringbootmvc.mapper.ArticleMapper;
import com.tedu.egspringbootmvc.pojo.Article;
import com.tedu.egspringbootmvc.pojo.dto.UserArticleDTO;
import com.tedu.egspringbootmvc.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
public class ArticleController {
    @Autowired
    private ArticleMapper articleMapper;
    @PostMapping("/v1/users/article")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");

            Article article = new Article();
            BeanUtils.copyProperties(userArticleDTO, article);
            article.setUserId(userVO.getId());
            articleMapper.insert(article);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);

    }
    @GetMapping("v1/users/selectByArticleId")
    public JsonResult selectByArticleId(@RequestParam int id){
        return JsonResult.success(articleMapper.selectByArticleId(id));
    }
}
